//
//  RootViewController.m
//  MobileOffice
//
//  Created by jjkj on 17/3/2.
//  Copyright © 2017年 liaoningjujin. All rights reserved.
//

#import "RootViewController.h"
#import "ViewController.h"

#import "FCBaseViewController.h"
#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYearForWeekOfYear)
@interface RootViewController ()

@end

@implementation RootViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  创建登录界面
 *
 */
+(LoginViewController *)createLoginView {

//    UIStoryboard * loginStoryboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    ViewController *loginView = [loginStoryboard instantiateViewControllerWithIdentifier:@"Login"];
    LoginViewController * loginView= [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    return loginView;
}

/**
 *  创建首页
 *
 */
+ (UINavigationController *)createHomeViewController
{
    FCBaseViewController *homeVC=[FCBaseViewController new];
    UINavigationController *homeNav=[[UINavigationController alloc]initWithRootViewController:homeVC];
    return homeNav;
}

/**
 *   本地推送
 *   content 推送内容
 *   userInfo
 *   type 0签到||1签退本地推送，签到签退提醒 2极光推送 3到达公出地点本地推送
 */
+(void)setLocalNotification:(NSString *)content userInfo:(NSDictionary *)userInfo type:(NSInteger)type
                       date:(NSString *)dateStr
{
    
    [self removeNotification];
    
    dateStr = [dateStr stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    NSDate * fireDate = [CommClass dateFormatTimeStamp:dateStr timeFormat:TIME_FORMATTER_24];

    //获取应用名称
    NSString * name = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    if (IOS_VERSION_10_OR_LATER) {
        //iOS10以上
        
        UNMutableNotificationContent *localNotContent = [[UNMutableNotificationContent alloc] init];
        localNotContent.sound = [UNNotificationSound defaultSound];
        localNotContent.body =content;
        localNotContent.userInfo = userInfo;
        localNotContent.title = name;
        if (type > 1) {
            //极光自定义消息本地推送
            UNTimeIntervalNotificationTrigger * timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"JPushlocation" content:localNotContent trigger:timeTrigger];
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            
            }];
        }else{
            //签到签退本地推送
            //设置是否重复发送，激光消息不重复，本地的重复
//            NSDateComponents* dateComp = [[NSDateComponents alloc] init];
//            dateComp.hour = fireDate.hour;
//            dateComp.minute = fireDate.minute;
             NSDateComponents * fireDateComp = [[NSCalendar currentCalendar] components:DATE_COMPONENTS fromDate:fireDate];
            UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:fireDateComp repeats:YES];

            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"signLocation" content:localNotContent trigger:trigger];
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                
            }];
        }
        
        
       
    }else{
        //ios10以下
        
        //设置是否重复发送，激光消息不重复，本地的重复
        NSCalendarUnit unit = NSCalendarUnitDay;
        if (type > 1) {
            fireDate = [NSDate date];
            unit = 0;
        }
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.userInfo = userInfo;
        localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber+1;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.alertBody = content;
        localNotification.fireDate = fireDate;
        
        localNotification.alertTitle = name;
        localNotification.timeZone=[NSTimeZone defaultTimeZone];
        
        // ios8后，需要添加这个注册，才能得到授权
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
        {
            UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            // 通知重复提示的单位，可以是天、周、月
            localNotification.repeatInterval = unit;
        }
        else
        {
            // 通知重复提示的单位，可以是天、周、月
            localNotification.repeatInterval = unit;
        }
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

}
/**移除本地推送*/
+(void)removeNotification
{
    if (IOS_VERSION_10_OR_LATER) {
        //iOS10以上
        [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
    }else{
        //ios10以下
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    }
    
}

@end
